package com.leyou.item.controller;

import com.leyou.common.dto.PageDTO;

import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.servcie.SkuService;
import com.leyou.item.servcie.SpecParamService;
import com.leyou.item.servcie.SpuDetailService;
import com.leyou.item.servcie.SpuService;
import com.leyou.pojo.dto.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/goods")
public class SpuAndSkuController {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuDetailService spuDetailService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpecParamService specParamService;

    /**
     * 分页查询spu
     * @param queryPageDto
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDto>> queryGoodsByPage(QueryPageDto queryPageDto){
       PageDTO<SpuDto>  pageDTO = spuService.queryGoodsByPage(queryPageDto);
    return ResponseEntity.ok(pageDTO);
    }

    /**
     * 新增商品
     * @param spuDto
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDto spuDto){
        spuService.saveGoods(spuDto);
        return ResponseEntity.status(201).build();
    }

    /**
     * 更改商品的上下架状态
     * @param spuId
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Long spuId,@RequestParam("saleable") Boolean saleable){
        spuService.updateSaleable(spuId,saleable);
        return ResponseEntity.status(200).build();
    }

    /**
     * 根据商品id查询商品的详细信息
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailsDto> querySpuDetail(@RequestParam("id") Long spuId){
        SpuDetail spuDetail = spuDetailService.getById(spuId);
        return ResponseEntity.ok(new SpuDetailsDto(spuDetail));
    }

    /**
     * 根据商品id查询商品
     * @param spuId
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDto>  querySpu(@PathVariable("id") Long spuId){
        Spu spu = spuService.getById(spuId);
        return ResponseEntity.ok(new SpuDto(spu));
    }

    /**
     * 更新商品
     * @return
     */
    @PutMapping
    public ResponseEntity<SpuDto> updateGoods(@RequestBody SpuDto spuDto){
         spuService.updateGoods(spuDto);
         return ResponseEntity.status(204).build();
    }

    /**
     * 根据id查询spu，spuDetail，sku三张表
     * @param spuId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDto>  querySkuAndSpuAndSpuDetail(@PathVariable("id") Long spuId){
        SpuDto spuDto = spuService.querySkuAndSpuAndSpuDetail(spuId);
        return ResponseEntity.ok(spuDto);
    }


    /**
     * 根据sku的id批量插叙sku
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDto>>  querySku(@RequestParam("ids") List<Long> ids){
        List<Sku> list = skuService.listByIds(ids);
        List<SkuDto> skuDtoList = list.stream().map(SkuDto::new).collect(Collectors.toList());
        return ResponseEntity.ok(skuDtoList);
    }

    /**
     * 根据spuId查询sku集合
     * @param spuId
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDto>> querySkuBySpuId(@RequestParam("id") Long spuId){
        List<SkuDto>  skuDtoList =skuService.querySkuBySpuId(spuId);
        return ResponseEntity.ok(skuDtoList);
    }


    /**
     * 查询规格参数值
     * @param spuId
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")

    public ResponseEntity<List<SpecParamDto>> queryParamValue(@RequestParam("id") Long spuId,
                                                        @RequestParam(value = "searching",required = false) Boolean searching){

        List<SpecParamDto> specParamDtoList = specParamService.queryParamValue(spuId,searching);
           return ResponseEntity.ok(specParamDtoList);
    }
}
